import 'package:flutter/material.dart';
import 'package:flutterclue/mvp/mode/login_mode.dart';
import 'package:flutterclue/utils/SharePreferensUtil.dart';
import 'package:flutterclue/utils/ShowSnackBarUtil.dart';
import 'package:flutterclue/utils/ToastUtilForAndroid.dart';
import 'MainCluePage.dart';
import 'package:flutterclue/mvp/presenter/login_presenter.dart';
import 'package:flutterclue/view/LoadingDialogView.dart';

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => new _LoginPageState();
}

class _LoginPageState extends State<LoginPage> implements LoginView {
  TextEditingController _editingControllerUser;
  TextEditingController _editingControllerPwd;
  LoginPresenter _loginPresenter = new LoginPresenter();

  ///判断是否登录
  _checkIsLogin() async {
    String token =
        await SharePreferencesUtil.getString(SharePreferencesUtil.TOKEN);
    if (token != null && token.isNotEmpty) {
      Navigator.of(context).pushAndRemoveUntil(
          new MaterialPageRoute(builder: (context) => new MainCluePage()),
          (route) => route == null);
    }
  }

  _goLogin() {
    if (_editingControllerPwd.text.toString().isEmpty) {
      showDialog(
          context: context,
          builder: (context) => AlertDialog(
                title: Text('密码不能为空'),
              ));
    } else if (_editingControllerUser.text.toString().isEmpty) {
      showDialog(
          context: context,
          builder: (context) => AlertDialog(
                title: Text('账号不能为空'),
              ));
    } else {
      _loginPresenter.login(
          account: _editingControllerUser.text.toString(),
          password: _editingControllerPwd.text.toString());
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _editingControllerUser.dispose();
    _editingControllerPwd.dispose();
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _checkIsLogin();
    _editingControllerPwd = new TextEditingController();
    _editingControllerUser = new TextEditingController();
    _loginPresenter.attachView(this);
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          new Padding(
              padding: new EdgeInsets.only(left: 20, top: 100),
              child: new Text('留修机器人',
                  style:
                      new TextStyle(color: Color(0xff333333), fontSize: 24.0))),
          new Padding(
            padding: new EdgeInsets.only(left: 16, top: 40, right: 16),
            child: new Row(
              children: <Widget>[
                Padding(
                  padding: new EdgeInsets.only(top: 20, bottom: 15),
                  child: new Text(
                    '账号',
                    style:
                        new TextStyle(color: Color(0xff333333), fontSize: 16.0),
                  ),
                ),
                new Expanded(
                    child: new TextField(
                  controller: _editingControllerUser,
                  decoration: new InputDecoration(
                      hintText: '请输入账号',
                      contentPadding:
                          new EdgeInsets.only(left: 15, top: 20, bottom: 15)),
                  autofocus: false,
                  maxLines: 1,
                  keyboardType: TextInputType.text,
                ))
              ],
            ),
          ),
          new Padding(
            padding: new EdgeInsets.only(left: 16, top: 20, right: 16),
            child: new Row(
              children: <Widget>[
                Padding(
                  padding: new EdgeInsets.only(top: 20, bottom: 15),
                  child: new Text(
                    '密码',
                    style:
                        new TextStyle(color: Color(0xff333333), fontSize: 16.0),
                  ),
                ),
                new Expanded(
                    child: new TextField(
                  obscureText: true,
                  controller: _editingControllerPwd,
                  decoration: new InputDecoration(
                      hintText: '请输入密码',
                      contentPadding:
                          new EdgeInsets.only(left: 15, top: 20, bottom: 15)),
                  maxLines: 1,
                ))
              ],
            ),
          ),
          new Padding(
            padding: new EdgeInsets.only(left: 16, top: 60, right: 16),
            child: new Row(
              children: <Widget>[
                new Expanded(
                  child: new RaisedButton(
                      padding: EdgeInsets.only(top: 15, bottom: 15),
                      color: Color(0xff0188fb),
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.all(Radius.circular(4))),
                      child: new Text(
                        '登录',
                        style: new TextStyle(color: Colors.white),
                      ),
                      onPressed: () {
                        _goLogin();
                      }),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }

  @override
  void onLoginFail(String error) {
    ToastUtil.showShort(error);
  }

  @override
  void onLoginSuccess(LoginBean loginBean) {
    _loginPresenter.saveJpush();
    Navigator.of(context).pushAndRemoveUntil(
        new MaterialPageRoute(builder: (context) => new MainCluePage()),
        (route) => route == null);
  }

  @override
  showLoadingView() {
    showDialog<Null>(
        context: context, //BuildContext对象
        barrierDismissible: false,
        builder: (BuildContext context) {
          return new LoadingDialog(
            //调用对话框
            text: '正在登录...',
          );
        });
  }

  @override
  dismissLoadingView() {
    Navigator.pop(context);
  }
}
